package com.icetech.eom.service.ws.cust;

import com.icetech.api.domain.ApiBaseRequest;
import com.icetech.api.domain.request.sms.SmsRequest;
import com.icetech.commonbase.JsonTools;
import com.icetech.commonbase.UUIDTools;
import com.icetech.commonbase.constants.CodeConstantsEnum;
import com.icetech.commonbase.domain.response.ObjectResponse;
import com.icetech.eom.common.constant.SecurityConstant;
import com.icetech.eom.config.redis.RedisUtils;
import com.icetech.eom.domain.response.WebServiceResponse;
import com.icetech.eom.domain.tx.Smslist;
import com.icetech.eom.service.smg.SmgSendServiceImpl;
import com.icetech.eom.service.tx.ISmslistService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发送短信接口
 * @author wangzw
 */
@Slf4j
@Component
public class SpringSendSmgServer {
    @Autowired
    private SmgSendServiceImpl smgService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private ISmslistService smslistService;

    @Transactional(rollbackFor = Exception.class)
    public String sendMsg(String para) throws Exception {
        log.info("<发送短信> 参数:{}",para);
        WebServiceResponse webServiceResponse = new WebServiceResponse();
        JSONObject jsonObject = new JSONObject(para);
        String mobileNo = jsonObject.get("mobileno").toString();
        String type = (String) jsonObject.get("type");
        String code = UUIDTools.getNonce_str(6);
        String content ="";
        if("1".equals(type)){
            content = "您的注册验证码: "+code+", 请在3分钟之内按页面提示提交验证码,切勿将验证码泄漏于他人.";
        }
        if("2".equals(type)){
            content = "您正在重置密码, 验证码: "+code+", 请在3分钟之内按页面提示提交验证码,切勿将验证码泄漏于他人.";
        }
        if("3".equals(type)){
            content = "该账号在其他手机上登录,需要验证码: "+code+", 非您本人允许,切勿将验证码泄漏于他人.";
        }
        SmsRequest smsRequest = new SmsRequest();
        smsRequest.setContent(content);
        smsRequest.setDesMobile(mobileNo);
        smsRequest.setTimes(60);
        ApiBaseRequest<SmsRequest> baseRequest = new ApiBaseRequest<SmsRequest>();
        baseRequest.setBizContent(smsRequest);
        ObjectResponse<String> response = smgService.sendMsg(baseRequest);
        if (!response.getCode().equals(CodeConstantsEnum.SUCCESS.getCode())){
            webServiceResponse.setState(-1+"");
            webServiceResponse.setRetmsg("短信发送异常");
            return JsonTools.toString(webServiceResponse);
        }
        //将验证码放入redis 有效期为5分钟
        redisUtils.set(SecurityConstant.WS_MSG_PRE + mobileNo, code, 60L*5);
        //添加短信记录信息
        Smslist smslist = new Smslist();
        smslist.setOprType(Integer.valueOf(type));
        smslist.setMobileNo(mobileNo);
        smslist.setContent(content);
        smslist.setResult(response.getCode());
        smslistService.save(smslist);
        webServiceResponse.setState(0+"");
        webServiceResponse.setRetmsg("");
        return JsonTools.toString(webServiceResponse);
    }

    @Transactional(rollbackFor = Exception.class)
    public String checkSmg(String para) throws JSONException {
        log.info("<短信验证> 参数:{}",para);
        WebServiceResponse webServiceResponse = new WebServiceResponse();
        JSONObject jsonObject = new JSONObject(para);
        String mobileNo = jsonObject.get("mobileno").toString();
        String vdsms = jsonObject.get("vdsms").toString();
        String code = redisUtils.get(SecurityConstant.WS_MSG_PRE + mobileNo).toString();
        if (StringUtils.isEmpty(code)){
            webServiceResponse.setState(3+"");
            webServiceResponse.setRetmsg("校验码不对或超时");
            return JsonTools.toString(webServiceResponse);
        }
        if (!code.equals(vdsms)){
            webServiceResponse.setState(3+"");
            webServiceResponse.setRetmsg("校验码不对或超时");
            return JsonTools.toString(webServiceResponse);
        }
        webServiceResponse.setState(0+"");
        webServiceResponse.setRetmsg("");
        return JsonTools.toString(webServiceResponse);
    }
}
